package com.robusta.spreadsheet.describe.jaxb;

import com.google.common.base.Function;
import com.robusta.spreadsheet.describe.TemplateInfo;
import com.robusta.spreadsheet.describe.WorkbookInfo;
import com.robusta.spreadsheet.describe.WorksheetInfo;

import javax.xml.bind.annotation.*;
import java.util.List;

import static com.google.common.collect.Lists.transform;
import static java.util.Collections.emptyList;

@XmlRootElement(namespace = "http://www.robusta-inc.com/describe-workbook")
@XmlAccessorType(XmlAccessType.FIELD)
public class Workbook implements WorkbookInfo {
    @XmlElement(namespace = "http://www.robusta-inc.com/describe-workbook")
    private Worksheets worksheets;


    public List<Worksheet> worksheets() {
        if(worksheets != null) {
            return worksheets.getWorksheets();
        }
        return emptyList();
    }

    @Override
    public List<TemplateInfo> templatesInfo() {
        return transform(worksheets(), new Function<Worksheet, TemplateInfo>() {
            @Override
            public TemplateInfo apply(Worksheet input) {
                return input.getTemplate();
            }
        });
    }

    public List<? extends WorksheetInfo> worksheetsInfo() {
        return worksheets();
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType
    public static class Worksheets {
        @XmlElement(name = "worksheet", namespace = "http://www.robusta-inc.com/describe-workbook")
        private List<Worksheet> worksheets;

        public List<Worksheet> getWorksheets() {
            return worksheets;
        }
    }
}
